﻿using System;
using System.IO;

namespace Flubu.Builds.Tasks
{
    public class GenerateCommonAssemblyInfoTask : TaskBase
    {
        public GenerateCommonAssemblyInfoTask(string productRootDir, Version buildVersion)
        {
            this.productRootDir = productRootDir;
            this.buildVersion = buildVersion;
        }

        public Version BuildVersion
        {
            get { return buildVersion; }
            set { buildVersion = value; }
        }

        public string ProductRootDir
        {
            get { return productRootDir; }
            set { productRootDir = value; }
        }

        public string CompanyName
        {
            get { return companyName; }
            set { companyName = value; }
        }

        public string ProductName
        {
            get { return productName; }
            set { productName = value; }
        }

        public string CompanyCopyright
        {
            get { return companyCopyright; }
            set { companyCopyright = value; }
        }

        public string CompanyTrademark
        {
            get { return companyTrademark; }
            set { companyTrademark = value; }
        }

        public string BuildConfiguration
        {
            get { return buildConfiguration; }
            set { buildConfiguration = value; }
        }

        public bool GenerateConfigurationAttribute
        {
            get { return generateConfigurationAttribute; }
            set { generateConfigurationAttribute = value; }
        }

        public bool GenerateCultureAttribute
        {
            get { return generateCultureAttribute; }
            set { generateCultureAttribute = value; }
        }

        public bool GenerateAssemblyVersion
        {
            get { return generateAssemblyVersion; }
            set { generateAssemblyVersion = value; }
        }

        public override string Description
        {
            get { return "Generate CommonAssemblyInfo.cs file"; }
        }

        protected override void DoExecute(ITaskContext context)
        {
            //TaskContext.LogTaskStarted("Generating CommonAssemblyInfo file");

            if (buildVersion == null)
            {
                context.Fail("Assembly file version is not set.");
                return;
            }

            using (Stream stream = File.Open(
                Path.Combine(productRootDir, "CommonAssemblyInfo.cs"), FileMode.Create))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine(
                        @"using System.Reflection;
using System.Runtime.InteropServices;

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: AssemblyCompanyAttribute(""{0}"")]
[assembly: AssemblyProductAttribute(""{1}"")]
[assembly: AssemblyCopyrightAttribute(""{2}"")]
[assembly: AssemblyTrademarkAttribute(""{3}"")]
[assembly: AssemblyFileVersionAttribute(""{4}"")]
[assembly: AssemblyInformationalVersionAttribute(""{5}"")]
[assembly: ComVisible(false)]",
                        companyName,
                        productName,
                        companyCopyright,
                        companyTrademark,
                        buildVersion,
                        buildVersion.ToString(2));

                    if (generateAssemblyVersion)
                        writer.WriteLine(@"[assembly: AssemblyVersionAttribute(""{0}.0.0"")]", buildVersion.ToString(2));

                    if (generateConfigurationAttribute)
                        writer.WriteLine(@"[assembly: AssemblyConfigurationAttribute(""{0}"")]", buildConfiguration);

                    if (generateCultureAttribute)
                        writer.WriteLine(@"[assembly: AssemblyCultureAttribute("""")]");
                }
            }

            //TaskContext.LogTaskFinished();
        }

        private Version buildVersion;
        private bool generateConfigurationAttribute;
        private bool generateCultureAttribute;
        private bool generateAssemblyVersion;
        private string productRootDir;
        private string companyName;
        private string productName;
        private string companyCopyright;
        private string companyTrademark;
        private string buildConfiguration;
    }
}
